﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebTraderLiteCL;

namespace WebTraderLite.Pages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["stock"] != null)
                {
                    tbStockSym.Text = Request.QueryString["stock"].ToUpper();
                }
            }
        }

        protected void showConfirmBuy(object sender, EventArgs e)
        {           
            //Show confirm buy view
            MultiViewBuyStock.ActiveViewIndex = 1;
            TextBox tbNumShares = fvBuyStock.FindControl("tbNumShares") as TextBox;
            Label lblNumShares = fvConfrimBuy.FindControl("lblNumShares") as Label;
            lblNumShares.Text = tbNumShares.Text;

            Stock stk = new Stock(tbStockSym.Text.Trim());
            float orderTotal = stk.Price * float.Parse(lblNumShares.Text);
            Label totPrice = fvConfrimBuy.FindControl("lblTotPrice") as Label;
            totPrice.Text = orderTotal.ToString("C");
        }

        protected void processBuy(object sender, EventArgs e)
        {
            
            Label lblSymbol = fvConfrimBuy.FindControl("lblStockSym") as Label;
            Stock stk = new Stock(lblSymbol.Text);
            Label lblNumShares = fvConfrimBuy.FindControl("lblNumShares") as Label;
            float numSharesToBuy = float.Parse(lblNumShares.Text);

            string username = System.Web.HttpContext.Current.User.Identity.Name.ToString();
            float totPrice = stk.Price * float.Parse(lblNumShares.Text);

            User usr = new User(username);
            //Validate user has enough money
            if (usr.getAccBalance() >= totPrice)
            {
                // if they have enough:
                //Add purchase of user stock to database
                //Debit user account
                DBTransactions trans = new DBTransactions();
                if (trans.inIntoUserStock(usr, stk, float.Parse(lblNumShares.Text)) == true)
                {
                    trans.debitBalance(usr, stk, float.Parse(lblNumShares.Text));
                    trans.insertIntoHistory(usr, stk, float.Parse(lblNumShares.Text), "BUY");
                    trans.deductandAddbrokerFees(usr.getUserID());
                    
                }

                Response.Redirect("~/Pages/Transactions.aspx");
            }
            else
            {
                Label lbl = new Label();
                lbl.Text = "You do not have enough money to Buy. Press cancel and try again.";
                
                lbl.Font.Bold = true;
                lbl.ForeColor = System.Drawing.Color.Red;
                //Display some sort of error message
                phlconfirm.Controls.Add(lbl);
            }
            
        }

        protected void cancelBuy(object sender, EventArgs e)
        {
            //Redirect back to original buy stock page
            Response.Redirect("~/Pages/BuyStock.aspx");
        }

        protected void tbStockSym_TextChanged(object sender, EventArgs e)
        {
            Stock stk = new Stock(tbStockSym.Text);
            if (stk.CompanyName.Equals("NULL"))
            {
                //Unknown symbol
                Label lbl = new Label();
                lbl.Text = "Stock sybmol " + stk.Symbol + " not found. ";
                lbl.Font.Bold = true;
                lbl.ForeColor = System.Drawing.Color.Red;
                phFailSearch.Controls.Add(lbl);
            }
        }

    }
}